package Client;

//TCPClient.java

import java.io.*;
import java.net.*;
import java.util.Scanner;

class RobotThread extends Thread
{
	SocketClient client;
	RobotThread(SocketClient sc){
		client = sc;
	}
	  public void run() {
		  try {
		       Thread.currentThread().sleep(2000);
		       client.act();
		       }
		     catch (InterruptedException e) {
		       e.printStackTrace();
		       }
      }
	
}

//while true
//given input from server create thread - waits and runs

class SocketClient extends Thread
{
	int serverNum1;
	int serverNum2;
	  
	Socket clientSocket; 
	RobotComponent robot;
	ControlComponent control;
	
	String ip;
	int port;
	boolean delay;
	
	SocketClient(ControlComponent con, String ip, int port, boolean delay){
	robot = con.getRobot();
	control = con;
	this.ip = ip;
	this.port = port;
	this.delay = delay;
	}
	
	/*
	 * There is likely a better way of doing this!
	 * We use threads here so the GUI can still update. Would not be required in non-hideous code.
	 */
	public void run()
	{
		try {
			this.receive();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
 public void receive() throws Exception
 {
	 try {
			clientSocket = new Socket(ip, port);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
  BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
     clientSocket.getInputStream()));
     
  while (true)
  {
    
    String fromServer = inFromServer.readLine();
    Scanner scan = new Scanner (fromServer);
    
    serverNum1 = scan.nextInt();
    serverNum2 = scan.nextInt();
    if(robot.getDelay()){
    RobotThread t = new RobotThread(this);
    t.start();
    }
    else
    	act();

    }
 }

public void act()
{
	   if (serverNum1 == -2)
       {
		   System.out.println("RECIEVED:" + serverNum1 + " + " + serverNum2);
		   try {
			   clientSocket.close();
		   } catch (IOException e) {
			   // TODO Auto-generated catch block
			   e.printStackTrace();
		   }
       }
   else if (serverNum1 == -1){
   	switch(serverNum2){
   	case 0:
   		robot.moveLeft();
   		robot.repaint();
   		break;
   	case 1:
   		robot.moveRight();
   		robot.repaint();
   		break;
   	case 2:
   		robot.rotate2CW();
   		robot.repaint();
   		break;
   	case 3:
   		robot.rotate2CCW();
   		robot.repaint();
   		break;
   	case 4:
   		robot.rotate3CW();
   		robot.repaint();
   		break;
   	case 5:
   		robot.rotate3CCW();
   		robot.repaint();
   		break;
   	case 6:
   		robot.invMoveLeft();
   		robot.repaint();
   		break;
   	case 7:
   		robot.invMoveRight();
   		robot.repaint();
   		break;
   	case 8:
   		robot.invMoveUp();
   		robot.repaint();
   		break;
   	case 9:
   		robot.invMoveDown();
   		robot.repaint();
   		break;
   	case 10:
   		robot.togglePaint();
   		robot.repaint();
   		break;
   	case 11:
   		robot.setPaint(true);
   		control.getPaintToggle().setSelected(true);
   		robot.repaint();
   		break;
   	case 12:
   		robot.setPaint(false);
   		control.getPaintToggle().setSelected(false);
   		robot.repaint();
   		break;
   	case 13:
   		robot.clear();
   		robot.repaint();
   		break;
   	case 14:
   		robot.reset();
   		robot.repaint();
   		break;
   	default:
   		System.out.println("ServerNum2 is not a valid int");
   	break;
   
   	}
   	System.out.println("RECIEVED:" + serverNum1 + " + " + serverNum2);
   }
   else
       {
        System.out.println("RECIEVED:" + serverNum1 + " + " + serverNum2);
        robot.getLinkMatrices().inverse_kinematics(serverNum1-175, 500-serverNum2);
        robot.getNewLinks();
        robot.repaint();
       }
}   

 
}
